package com.lmoumou.lib_calendarview.widget

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.PagerSnapHelper
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import com.lmoumou.lib_calendarview.entity.DateBeen
import com.lmoumou.lib_calendarview.entity.DayAttrsBeen
import com.lmoumou.lib_calendarview.entity.DayBeen

/**
 * @author:Lmoumou
 * @date:2019/11/5
 * 描述:
 **/
class CalendarView : RecyclerView {
    companion object {
        const val currentPosition = 1000
        private const val TAG = "CalendarView"
    }

    var listener: OnItemClickListener? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int = 0) : super(context, attrs, defStyle) {
        layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        val helper = object : PagerSnapHelper() {
            override fun findTargetSnapPosition(layoutManager: LayoutManager?, velocityX: Int, velocityY: Int): Int {
                return super.findTargetSnapPosition(layoutManager, velocityX, velocityY)
            }
        }
        helper.attachToRecyclerView(this)
        adapter = calendarAdapter
        (layoutManager as LinearLayoutManager).scrollToPosition(currentPosition)
    }

    private val dataList by lazy { mutableListOf<DateBeen>() }
    private val calendarAdapter: CalendarAdapter by lazy {
        CalendarAdapter(context) { data, position1, position2 ->
            listener?.onItemClick(data, position1, position2)
        }
    }

    fun setSpecialMap(map: LinkedHashMap<String, DayAttrsBeen>) {
        calendarAdapter.setSpecialMap(map)
    }

    interface OnItemClickListener {
        fun onItemClick(data: DayBeen, position1: Int, position2: Int)
    }
}